<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <link   href="../css/estilos.css" rel="stylesheet">
    <script src="../js/bootstrap.js"></script>
	<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
    <script type="text/javascript" src="../js/alertas.js"></script>  

	<style>
        #alerta{
            display: none;
            width:100%;
            top:0px;
            left:0px;
            z-index: 9999999;
            background-color: #920000;
            position:fixed;
            padding:15px 50px;
            cursor:pointer;
            
            /*estas propriedades são opicionais*/
            font-family: 'trebuchet ms';
            font-size: 18px;
            text-align: center;
            color: #fff;
        
            /*uma pequena sombra para que seja possível destacar o alrta do resto da página*/
            -moz-box-shadow: 0px 2px 2px #888;
            -webkit-box-shadow: 0px 2px 2px #888;
            box-shadow: 0px 2px 2px #888;
        }
    </style>
</head>
 
<body>
<?php
	if ( !empty($_POST)) {
	session_start();
	
	include '../class/database.php';
	$pdo = Database::connect();
		
	try {
		$usuario = $_POST['usuario'];//tras o que foi digitado no campo "usuario" do form
		$salt = '1%1cAu!g+>K53PY}';	
		$senha = $_POST['senha'];
		$senha = md5($senha . $salt);
		
	
		$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING);
		$sql = $pdo->prepare("SELECT * FROM usuarios WHERE usuario='$usuario' AND senha='$senha'");
		$sql->execute();
		$num = $sql->rowCount();
		if($num >0){
			echo '<script>top.location.href="../index.php"</script>';
			$_SESSION['usuario']=$_POST['usuario'];
			$_SESSION['senha']=$_POST['senha'];
		}else{
			echo '
				<script language>
					$(function(){
						alerta("Usuário ou senha Incorreto. Tente novamente!");
					}); 
				</script>';
		}
	} 
		catch (PDOException $erro) {
		echo $erro->getMessage();
	}
	}
		
?>  
    <div class="container">        
        <div class="row" style="margin-top:20px">
            <div class="col-xs-12 col-sm-8 col-md-6 col-sm-offset-2 col-md-offset-3">            
                <form method="post" role="form" action="">
                    <fieldset>
                    	<center><img src="../img/cafe-jornal-logo.png" /></center>
                        <hr class="colorgraph">
                        <div class="form-group">
                            <input type="text" name="usuario" id="email" class="form-control input-lg" placeholder="Usuário" data-required="true" data-pattern="^[a-zA-Z0-9._-]{3,16}" data-describedby="usuario" data-description="valida">
                            <div id="usuario"></div><!--Aparece alerta-->
                        </div>
                        <div class="form-group">
                            <input type="password" name="senha" id="password" class="form-control input-lg" placeholder="Senha" data-required="true" data-pattern="^[a-zA-Z0-9._-]{3,16}" data-describedby="senha" data-description="valida">
                            <div id="senha"></div><!--Aparece alerta-->
                        </div>
                        <hr class="colorgraph">
                        <div class="row">
                            <div class="col-xs-12 col-sm-8 col-md-6 col-sm-offset-2 col-md-offset-3">
                                <input type="submit" class="btn btn-lg btn-success btn-block" value="Entrar">
                            </div>
                        </div>
                    </fieldset>
                </form>
            </div><!--col-xs-12-->
        </div><!--row-->  
    </div><!--Container-->  
    
    
    <script src="../js/jquery-validate.js"></script>

    <script>
        $('form').validate({
                onKeyup : true,
                sendForm : true,
                eachValidField : function() {

                        $(this).closest('div').removeClass('error').addClass('success');
                        $(this).css('background', '#fff');
                },
                eachInvalidField : function() {

                        $(this).closest('div').removeClass('success').addClass('error');
                        $(this).css('background', '#d9534f');
                },
                description : {
                        valida : {
                                required : '<div style="margin:10px 0; padding:10px" class="badge badge-important"><i class="fa fa-ban"></i> Campo Obrigatório</div>',
                                pattern : '<div style="margin:10px 0; padding:10px" class="badge badge-warning"><i class="fa fa-exclamation-circle"></i> Preenchimento Incorreto.</div>',
                                conditional : '<div style="margin:10px 0; padding:10px" class="badge badge-important"><i class="fa fa-exclamation-circle"></i> Conditional</div>',
                                valid : '<div style="margin:10px 0; padding:10px" class="badge badge-success"><i class="fa fa-check-circle"></i> Validado</div>'
                        }
                }
        });
    </script>  
      
  </body>
</html>

